﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.ServiceModel.Channels;

using SBPweb.PlAzureNet.Shared.Library.Constants;

namespace System.ServiceModel
{

    public static class ClientBaseExtenders
    {

        public static void CallInContext<TClient>(this ClientBase<TClient> extendee, Action action, params object[] args)
            where TClient : class
        {
            CallInContext<TClient>(extendee, new List<KeyValuePair<string, string>>() { new KeyValuePair<string, string>("culture", CultureInfo.CurrentCulture.Name) }, action, args);
        }

        public static void CallInContext<TClient>(this ClientBase<TClient> extendee, IEnumerable<KeyValuePair<string, string>> headerKeyValues, Action action, params object[] args)
            where TClient : class
        {
            using (OperationContextScope ocs = new OperationContextScope(extendee.InnerChannel))
            {
                foreach (var kvp in headerKeyValues)
                {
                    OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader(kvp.Key, Namespaces.Services_Headers_Culture, kvp.Value));
                }
                action.DynamicInvoke(args);
            }
        }

        public static void CallInContext<TClient, TParam0>(this ClientBase<TClient> extendee, Action<TParam0> action, TParam0 arg0)
            where TClient : class
        {
            CallInContext<TClient, TParam0>(extendee, new List<KeyValuePair<string, string>>() { new KeyValuePair<string, string>("culture", CultureInfo.CurrentCulture.Name) }, action, arg0);
        }

        public static void CallInContext<TClient, TParam0>(this ClientBase<TClient> extendee, IEnumerable<KeyValuePair<string, string>> headerKeyValues, Action<TParam0> action, TParam0 arg0)
            where TClient : class
        {
            using (OperationContextScope ocs = new OperationContextScope(extendee.InnerChannel))
            {
                foreach (var kvp in headerKeyValues)
                {
                    OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader(kvp.Key, Namespaces.Services_Headers_Culture, kvp.Value));
                }
                action.Invoke(arg0);
            }
        }


        public static void CallInContext<TClient, TParam0, TParam1>(this ClientBase<TClient> extendee, Action<TParam0, TParam1> action, TParam0 arg0, TParam1 arg1)
            where TClient : class
        {
            CallInContext<TClient, TParam0, TParam1>(extendee, new List<KeyValuePair<string, string>>() { new KeyValuePair<string, string>("culture", CultureInfo.CurrentCulture.Name) }, action, arg0, arg1);
        }

        public static void CallInContext<TClient, TParam0, TParam1>(this ClientBase<TClient> extendee, IEnumerable<KeyValuePair<string, string>> headerKeyValues, Action<TParam0, TParam1> action, TParam0 arg0, TParam1 arg1)
            where TClient : class
        {
            using (OperationContextScope ocs = new OperationContextScope(extendee.InnerChannel))
            {
                foreach (var kvp in headerKeyValues)
                {
                    OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader(kvp.Key, Namespaces.Services_Headers_Culture, kvp.Value));
                }
                action.Invoke(arg0, arg1);
            }
        }


        public static void CallInContext<TClient, TParam0, TParam1, TParam2>(this ClientBase<TClient> extendee, Action<TParam0, TParam1, TParam2> action, TParam0 arg0, TParam1 arg1, TParam2 arg2)
            where TClient : class
        {
            CallInContext<TClient, TParam0, TParam1, TParam2>(extendee, new List<KeyValuePair<string, string>>() { new KeyValuePair<string, string>("culture", CultureInfo.CurrentCulture.Name) }, action, arg0, arg1, arg2);
        }

        public static void CallInContext<TClient, TParam0, TParam1, TParam2>(this ClientBase<TClient> extendee, IEnumerable<KeyValuePair<string, string>> headerKeyValues, Action<TParam0, TParam1, TParam2> action, TParam0 arg0, TParam1 arg1, TParam2 arg2)
            where TClient : class
        {
            using (OperationContextScope ocs = new OperationContextScope(extendee.InnerChannel))
            {
                foreach (var kvp in headerKeyValues)
                {
                    OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader(kvp.Key, Namespaces.Services_Headers_Culture, kvp.Value));
                }
                action.Invoke(arg0, arg1, arg2);
            }
        }


        public static void CallInContext<TClient, TParam0, TParam1, TParam2, TParam3>(this ClientBase<TClient> extendee, Action<TParam0, TParam1, TParam2, TParam3> action, TParam0 arg0, TParam1 arg1, TParam2 arg2, TParam3 arg3)
            where TClient : class
        {
            CallInContext<TClient, TParam0, TParam1, TParam2, TParam3>(extendee, new List<KeyValuePair<string, string>>() { new KeyValuePair<string, string>("culture", CultureInfo.CurrentCulture.Name) }, action, arg0, arg1, arg2, arg3);
        }

        public static void CallInContext<TClient, TParam0, TParam1, TParam2, TParam3>(this ClientBase<TClient> extendee, IEnumerable<KeyValuePair<string, string>> headerKeyValues, Action<TParam0, TParam1, TParam2, TParam3> action, TParam0 arg0, TParam1 arg1, TParam2 arg2, TParam3 arg3)
            where TClient : class
        {
            using (OperationContextScope ocs = new OperationContextScope(extendee.InnerChannel))
            {
                foreach (var kvp in headerKeyValues)
                {
                    OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader(kvp.Key, Namespaces.Services_Headers_Culture, kvp.Value));
                }
                action.Invoke(arg0, arg1, arg2, arg3);
            }
        }

    }

}